/*
 * ImageIcon.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import java.net.URL;

import com.cute4j.swing.graphics.Graphics;
import com.cute4j.swing.graphics.Image;

/**
 * Image Icon.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ImageIcon implements Icon {

// Static Initialization. ------------------------------------------------------

  static {
    Swing.initialize();
  }

// Package-Private Fields. -----------------------------------------------------

  final Image image;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new ImageIcon object.
   *
   * @param image the image
   */
  public ImageIcon(Image image) {
    this.image = image;
  }

  /**
   * Constructs new ImageIcon object.
   *
   * @param fileName the file name
   */
  public ImageIcon(String fileName) {
    this(Swing.crateImage(fileName));
  }

  /**
   * Constructs new ImageIcon object.
   *
   * @param location the location
   */
  public ImageIcon(URL location) {
    this(Swing.crateImage(location));
  }

  /**
   * Constructs new ImageIcon object.
   *
   * @param imageData the image data
   */
  public ImageIcon(byte[] imageData) {
    this(Swing.crateImage(imageData));
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns image.
   *
   * @return image
   */
  public Image getImage() {
    return image;
  }

// Icon Interface Implementation. ----------------------------------------------

  /**
   * {@inheritDoc}
   */
  public int getIconWidth() {
    return image.getWidth();
  }

  /**
   * {@inheritDoc}
   */
  public int getIconHeight() {
    return image.getHeight();
  }

  /**
   * {@inheritDoc}
   */
  public void paintIcon(Component component, Graphics g, int x, int y) {
    g.drawImage(image, x, y);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if ((obj != null) && (obj.getClass() == getClass())) {
      ImageIcon imageIcon = (ImageIcon)obj;
      return image.equals(imageIcon.image);
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  public int hashCode() {
    return image.hashCode();
  }

}
